package perf.store;

import java.io.File;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class StoreManager implements ServletContextListener {

	private static ObjectStore store;
	
	public void contextInitialized(ServletContextEvent arg0) {
		String path = System.getenv("PERFECT_DB_PATH");
		if ( path == null ) {
			throw new RuntimeException( "The environment variable PERFECT_DB_PATH is not set" );
		}
		File db = new File( path );
		if ( ! db.exists() ) {
			ObjectStore.create( db );
		}
		
		store = new ObjectStore( db );
		
		System.out.println( "STARTED DB4O DATABASE" );
	}

	public void contextDestroyed(ServletContextEvent arg0) {
		store.close();
		System.out.println( "STOPPED DB4O DATABASE" );
	}
	
	public static ObjectStore store() {
		return store;
	}
}
